<template>
  <div class="body">
    <div class="logo_panel">
      <div class="icon">注册账户</div>
    </div>
    <div class="login_panel">
      <el-form :model="MyForm" ref="MyForm">
        <el-form-item prop="user_email" :rules="email_rules">
          <el-input
            v-model="MyForm.user_email"
            placeholder="请输入邮箱"
          ></el-input>
        </el-form-item>
        <el-form-item prop="user_loginname" :rules="input_rules">
          <el-input
            v-model="MyForm.user_loginname"
            placeholder="请输入用户名"
          ></el-input>
        </el-form-item>
        <el-form-item prop="user_loginpass" :rules="input_rules">
          <el-input
            v-model="MyForm.user_loginpass"
            placeholder="请输入密码"
            show-password
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button @click="regUser('MyForm')">注册账号</el-button>
          <el-button class="back" @click="goLogin()">登  录</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>
<script>
export default {
  name: "RegView",
  data() {
    return {
      MyForm: {
        user_loginname: "",
        user_loginpass: "",
        user_email: "",
      },
      email_rules: [
        { required: true, message: "请输入邮箱地址", trigger: "blur" },
        {
          type: "email",
          message: "请输入正确的邮箱地址",
          trigger: ["blur", "change"],
        },
      ],
      input_rules: [
        { required: true, message: "请输入", trigger: "blur" },
      ],
    };
  },
  mounted() {},
  methods: {
    regUser(formName) {
        //先验证输入
        this.$refs[formName].validate((valid) => {
          if (valid) {
            this.RegPost();
            //console.log('submit!!');
          } else {
            //console.log('error submit!!');
            return false;
          }
        });    
    },
    async RegPost(){
        let res = await this.$http.post("regUser", this.MyForm);
      if (res.state == 200) {
        this.$message({
          message: res.message,
          type: "success",
        });
        this.$router.push("/login");
      } else {
        this.$message.error(res.message);
        this.$router.push("/regView");
      }
    },
    goLogin() {
      this.$router.push("/login");
    },
  },
};
</script>
<style scoped>
.body {
  background-color: #1678ff;
  height: 100vh;
}

.logo_panel {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 100px 0;
  color: #ffffff;
}

.logo_panel .icon svg {
  width: 120px;
  height: 120px;
  fill: #ffffff;
}

/* 登录窗口 */
.login_panel {
  display: flex;
  flex-direction: column;
  align-items: center;
}

.login_panel .el-input {
  outline: none;
  height: 40px;
  width: 250px;
  font-size: 18px;
  color: #000000;
  border-radius: 4px;
}

.login-btn {
  padding: 20px 0;
}

.el-button {
  display: inline-block;
  line-height: 1;
  white-space: nowrap;
  cursor: pointer;
  background: #fff;
  border: 1px solid #dcdfe6;
  color: #606266;
  -webkit-appearance: none;
  text-align: center;
  box-sizing: border-box;
  outline: none;
  margin: 0;
  transition: 0.1s;
  font-weight: 500;
  -moz-user-select: none;
  -webkit-user-select: none;
  -ms-user-select: none;
  padding: 12px 20px;
  font-size: 14px;
  border-radius: 4px;
}

.mybtn {
  width: 120px;
}

.back {
  width: 98px;
  margin-left: 20px;
}

.el-form-item {
  text-align: center;
}
</style>